//
//  AnnouncementViewController.m
//  Kjs-iOS
//
//  Created by Pat on 16/8/5.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "AnnouncementViewController.h"
#import "MJRefresh.h"
#import "KJSRequest.h"
#import "AppDelegate.h"
#import "MBProgressHUD+MAP.h"
#import "AnnouncementDetailViewController.h"
#import "GUATool.h"
@interface AnnouncementViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray<MessageList *> * announcements;
@property (strong, nonatomic) NSMutableArray<NewsList *> * newsList;
@property (assign, nonatomic) NSInteger pageIndex;
@end

@implementation AnnouncementViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    switch (self.announcementType) {
        case 0:
            self.navigationItem.title = @"系统消息";
            break;
        case 1:
            self.navigationItem.title = @"平台动态";
            break;
        case 2:
            self.navigationItem.title = @"业内新闻";
            break;
        default:
            break;
    }
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 44.f;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    
    [self refresh];
}

- (NSMutableArray *)announcements {
    if (!_announcements) {
        _announcements = [[NSMutableArray alloc] init];
    }
    return _announcements;
}

- (void)refresh {
    [self.tableView.mj_footer resetNoMoreData];
    switch (self.announcementType) {
        case 0: {
            //系统消息
            [CurentDelegate getAccountMessageAt:self ret:^(UserInfo *account) {
                [KJSRequest getAppMassMessageWithPartyId:account.partyId
                                                  inPage:1
                                                  ofRows:10
                                                     ret:^(MassMessageModel *model) {
                                                         if (model.success) {
                                                             self.announcements = [model.messageList mutableCopy];
                                                             [self.tableView reloadData];
                                                             self.pageIndex = 1;
                                                         } else {
                                                             [MBProgressHUD showError:@"获取公告列表失败"];
                                                         }
                                                         [self.tableView.mj_header endRefreshing];
                                                     }];
            }];
            break;
        }
        case 1: {
            //平台动态
            [KJSRequest showTrendsListForAppAtPage:@"1"
                                              rows:@"10"
                                               ret:^(NewsListModel *model) {
                                                   if (model.success) {
                                                       self.newsList = [model.platformTrendsList mutableCopy];
                                                       [self.tableView reloadData];
                                                       self.pageIndex = 1;
                                                   } else {
                                                       [MBProgressHUD showError:@"获取平台动态列表失败"];
                                                   }
                                                   [self.tableView.mj_header endRefreshing];
                                               }];
            break;
        }
        case 2: {
            //业内新闻
            [KJSRequest showNewsListForAppAtPage:@"1"
                                            rows:@"10"
                                             ret:^(NewsListModel *model) {
                                                 if (model.success) {
                                                     self.newsList = [model.platformNewsList mutableCopy];
                                                     [self.tableView reloadData];
                                                     self.pageIndex = 1;
                                                 } else {
                                                     [MBProgressHUD showError:@"获取业内新闻列表失败"];
                                                 }
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
        }
        default:
            break;
    }
}

- (void)loadMore {
    
    switch (self.announcementType) {
        case 0: {
            //系统消息
            [CurentDelegate getAccountMessageAt:self ret:^(UserInfo *account) {
                [KJSRequest getAppMassMessageWithPartyId:account.partyId
                                                  inPage:++self.pageIndex
                                                  ofRows:10
                                                     ret:^(MassMessageModel *model) {
                                                         if (model.success) {
                                                             [self.announcements addObjectsFromArray:model.messageList];
                                                             [self.tableView reloadData];
                                                             if (self.pageIndex > model.totalpages) {
                                                                 [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                                             } else {
                                                                 [self.tableView.mj_footer endRefreshing];
                                                             }
                                                         } else {
                                                             [MBProgressHUD showError:@"获取更多公告失败"];
                                                             --self.pageIndex;
                                                         }
                                                     }];
            }];
            break;
        }
        case 1: {
            //平台动态
            [KJSRequest showTrendsListForAppAtPage:[NSString stringWithFormat:@"%ld",  ++self.pageIndex]
                                              rows:@"10"
                                               ret:^(NewsListModel *model) {
                                                   if (model.success) {
                                                       [self.newsList addObjectsFromArray:model.platformTrendsList];
                                                       [self.tableView reloadData];
                                                       if (self.pageIndex > model.totalpages) {
                                                           [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                                       } else {
                                                           [self.tableView.mj_footer endRefreshing];
                                                       }
                                                   } else {
                                                       [MBProgressHUD showError:@"获取更多平台动态失败"];
                                                       --self.pageIndex;
                                                   }
                                               }];
            break;
        }
        case 2: {
            //业内新闻
            [KJSRequest showNewsListForAppAtPage:[NSString stringWithFormat:@"%ld",  ++self.pageIndex]
                                            rows:@"10"
                                             ret:^(NewsListModel *model) {
                                                 if (model.success) {
                                                     [self.newsList addObjectsFromArray:model.platformNewsList];
                                                     [self.tableView reloadData];
                                                     if (self.pageIndex > model.totalpages) {
                                                         [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                                     } else {
                                                         [self.tableView.mj_footer endRefreshing];
                                                     }
                                                 } else {
                                                     [MBProgressHUD showError:@"获取更多业内新闻失败"];
                                                     --self.pageIndex;
                                                 }
                                             }];
            break;
        }
        default:
            break;
    }
}

#pragma mark - tableview config
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.announcementType == 0? self.announcements.count : self.newsList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@"announcement"];
    UILabel * title = [cell viewWithTag:1001];
    UILabel * time = [cell viewWithTag:1002];
    switch (self.announcementType) {
        case 0:
            [title setText:self.announcements[indexPath.row].userMessageTitle];
            [time setText:[NSString stringWithFormat:@"%@", self.announcements[indexPath.row].createdTxStamp]];
            break;
        case 1:
        case 2:
            [title setText:self.newsList[indexPath.row].title];
            [time setText:[NSString stringWithFormat:@"%@",  self.newsList[indexPath.row].createdTxStamp]];
        default:
            break;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.separatorInset = UIEdgeInsetsZero;
    cell.layoutMargins = UIEdgeInsetsZero;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
    AnnouncementDetailViewController * announcementDetailVC = [[AnnouncementDetailViewController alloc] initWithViewControllerIdentifier:@"AnnouncementDetailViewController"];
    switch (self.announcementType) {
        case 0: {
            [KJSRequest getAppMassMessageDetailWithUserReceiveMessageId:self.announcements[indexPath.row].userReceiveMessageId
                                                                    ret:^(MassMessageDetailModel *model) {
                                                                        if (model.success) {
                                                                            announcementDetailVC.announcementType = 0;
                                                                            announcementDetailVC.messageDetail = model.messagedetail[0];
                                                                            [self displayViewController:announcementDetailVC];
                                                                        } else {
                                                                            [MBProgressHUD showError:@"获取公告详情失败"];
                                                                        }
                                                                        [cell setSelected:NO];
                                                                    }];
            break;
        }
        case 1: {
            [KJSRequest getTrendsDetailsForAppWithPlatformTrendsId:self.newsList[indexPath.row].platformTrendsId
                                                               ret:^(NewsListModel *model) {
                                                                   if (model.success) {
                                                                       announcementDetailVC.announcementType = 1;
                                                                       announcementDetailVC.newsDetail = model.platformTrendsList[0];
                                                                       [self displayViewController:announcementDetailVC];
                                                                   } else {
                                                                       [MBProgressHUD showError:@"获取平台动态详情失败"];
                                                                   }
                                                                   [cell setSelected:NO];
                                                               }];
            break;
        }
        case 2: {
            [KJSRequest getNewsDetailsForAppWithPlatformNewsId:self.newsList[indexPath.row].platformNewsId
                                                           ret:^(NewsListModel *model) {
                                                               if (model.success) {
                                                                   announcementDetailVC.announcementType = 2;
                                                                   announcementDetailVC.newsDetail = model.result[0];
                                                                   [self displayViewController:announcementDetailVC];
                                                               } else {
                                                                   [MBProgressHUD showError:@"获取业内新闻详情失败"];
                                                               }
                                                               [cell setSelected:NO];
                                                           }];
            break;
        }
        default:
            break;
    }
}
@end
